﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MiserBlogMVC.Filter;
using MiserBlogHelper.Helper;
using System.Drawing;

namespace MiserBlogMVC.HTTP
{
    [AuthorUserVIPFilter]
    public class upload_image : BaseHTTPHandler
    {
        private HttpPostedFile image_file = null;

        protected override bool AuthVarEvent(HttpContext context)
        {
            image_file = context.Request.Files["imgFile"];
            if (image_file == null)
            {
                return false;
            }
            return true;
        }

        protected override bool ExecEvent(HttpContext context)
        {
            string imageUrl = request_person.Id + "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            string savePath = ImageHelper.BlogPublishPhysicsPath + imageUrl;
            string imageName = Guid.NewGuid().ToString() + ImageHelper.GetImageExtendName(image_file.ContentType);

            if (!System.IO.Directory.Exists(savePath))
            {
                System.IO.Directory.CreateDirectory(savePath);
            }

            image_file.SaveAs(savePath + imageName);

            resultJSON = Newtonsoft.Json.JsonConvert.SerializeObject(new { error = 0, url = ImageHelper.BlogPublishRelativePath + imageUrl + imageName });

            return true;
        }
    }
}